package cn.edu.sbs.talent.shiro.service.impl;

import cn.edu.sbs.talent.common.service.impl.BaseService;
import cn.edu.sbs.talent.shiro.entity.Role;
import cn.edu.sbs.talent.shiro.mapper.RoleMapper;
import cn.edu.sbs.talent.shiro.service.IRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * RoleService
 *
 * @author ctidy
 * @since 2021/1/7
 */
@Service
@Transactional
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class RoleService extends BaseService<Role> implements IRoleService {

    private final RoleMapper mapper;

    @Override
    public List<Role> listByAccount(long id) {
        return mapper.selectBatchByAccountId(id);
    }

    @Override
    public List<Role> listByAccount(String username) {
        Assert.hasLength(username, "用户名不能为空");
        return mapper.selectBatchByAccountUsername(username);
    }

    @Override
    public Set<String> listNameByAccount(long id) {
        return listByAccount(id).stream().map(Role::getName).collect(Collectors.toSet());
    }

    @Override
    public Set<String> listNameByAccount(String username) {
        return listByAccount(username).stream().map(Role::getName).collect(Collectors.toSet());
    }
}
